package com.example.cofeed.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.blankj.utilcode.util.SPUtils;
import com.example.cofeed.R;
import com.example.cofeed.base.BaseActivity;
import com.example.cofeed.constant.SputilsConstant;
import com.example.cofeed.entity.CardBean;
import com.example.cofeed.even.HomeRefreshEven;
import com.umeng.analytics.MobclickAgent;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by mqwang on 2017/9/26.
 */

public class SetFontActivity extends BaseActivity {
    @BindView(R.id.title_font_tv)
    TextView titleFontTv;
    @BindView(R.id.content_font_tv)
    TextView contentFontTv;
    private OptionsPickerView pvCustomOptions;
    private ArrayList<CardBean> cardItem = new ArrayList<>();
    private int headingFontSize;
    private int contentFontSize;
    private int type;
    private boolean isSet;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_set_font;
    }

    @Override
    protected void init(Bundle savedInstanceState, View container) {
        setMainTitle("字体大小");
        headingFontSize = SPUtils.getInstance().getInt(SputilsConstant.HEADING_FONT_SIZE, 0);
        contentFontSize = SPUtils.getInstance().getInt(SputilsConstant.CONTENT_FONT_SIZE, 0);
        //小：small、标准：mid、大：big、极大：high、超大：max
        switch (headingFontSize){
            case 0:
                titleFontTv.setText("小");
                break;
            case 1:
                titleFontTv.setText("标准");
                break;
            case 2:
                titleFontTv.setText("大");
                break;
            case 3:
                titleFontTv.setText("极大");
                break;
            case 4:
                titleFontTv.setText("超大");
                break;
        }
        switch (contentFontSize){
            case 0:
                contentFontTv.setText("标准");
                break;
            case 1:
                contentFontTv.setText("中");
                break;
            case 2:
                contentFontTv.setText("大");
                break;
            case 3:
                contentFontTv.setText("极大");
                break;
            case 4:
                contentFontTv.setText("超大");
                break;
        }
        initCustomOptionPicker();

    }

    private void initCustomOptionPicker() {//条件选择器初始化，自定义布局
        cardItem.add(new CardBean(0, "标准"));
        cardItem.add(new CardBean(1, "中"));
        cardItem.add(new CardBean(2, "大"));
        cardItem.add(new CardBean(3, "极大"));
        cardItem.add(new CardBean(4, "超大"));

        // 注意：自定义布局中，id为 optionspicker 或者 timepicker 的布局以及其子控件必须要有，否则会报空指针
        // 具体可参考demo 里面的两个自定义布局
        pvCustomOptions = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3, View v) {
                //返回的分别是三个级别的选中位置
                String tx = cardItem.get(options1).getPickerViewText();
                switch (type){
                    case 0:
                        titleFontTv.setText(tx);
                        SPUtils.getInstance().put(SputilsConstant.HEADING_FONT_SIZE, options1);
                        break;
                    case 1:
                        contentFontTv.setText(tx);
                        SPUtils.getInstance().put(SputilsConstant.CONTENT_FONT_SIZE, options1);
                        break;
                }
                isSet = true;

            }
        })
                .setTitleText("字体大小")
                .build();

        pvCustomOptions.setPicker(cardItem);//添加数据

    }

    @OnClick({R.id.title_font_layout, R.id.content_font_layout})
    public void onViewClicked(View view) {
        pvCustomOptions.show();
        switch (view.getId()) {
            case R.id.title_font_layout:
                pvCustomOptions.setSelectOptions(headingFontSize);
                type = 0;
                break;
            case R.id.content_font_layout:
                pvCustomOptions.setSelectOptions(contentFontSize);
                type = 1;
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (isSet) {
            EventBus.getDefault().post(new HomeRefreshEven());
        }

    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

}
